package com.chain.BrotherExchange.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.AppCompatImageView;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.ui.core.BaseActivity;

import butterknife.Bind;

public class WebViewActivity extends BaseActivity {

    @Bind(R.id.web_view) WebView mWebView;
    @Bind(R.id.title_text) TextView mTitleTextView;
    @Bind(R.id.progress_bar) ProgressBar mProgressBar;
    @Bind(R.id.arrow_back_image) AppCompatImageView mArrowBackImage;

    /**
     * 关于我们
     */
    public static void aboutUs(Context context){
        String url = "http://www.gbcoin.top/mobile/article?idx=1";
        launch(context, url);
    }

    /**
     * 服务条款
     */
    public static void termsService(Context context){
        String url = "http://www.gbcoin.top/mobile/article?idx=5";
        launch(context, url);
    }

    /**
     * 隐私保护
     */
    public static void privacyProtection(Context context){
        String url = "http://www.gbcoin.top/mobile/article?idx=7";
        launch(context, url);
    }

    /**
     * 免责声明
     */
    public static void freeStatement(Context context){
        String url = "http://www.gbcoin.top/mobile/article?idx=6";
        launch(context, url);
    }

    public static void launch(Context context, String url) {
        context.startActivity(new Intent(context, WebViewActivity.class)
                .putExtra(C.EXTRA_WEB_URL, url));
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initActions() {
        mArrowBackImage.setOnClickListener(v -> onBackPressed());

        initSetting(this, mWebView);
        mWebView.loadUrl(getIntent().getStringExtra(C.EXTRA_WEB_URL));

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (mTitleTextView != null) {
                    mTitleTextView.setText(title);
                }
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (mProgressBar != null) {
                    mProgressBar.setProgress(newProgress);
                    if (newProgress == 100) {
                        mProgressBar.setVisibility(View.GONE);
                    }
                }
            }
        });

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Build.VERSION.SDK_INT < 26 && mWebView != null) {
                    mWebView.loadUrl(url);
                    return true;
                }

                return false;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean b = mWebView.canGoBack();
        if ((keyCode == KeyEvent.KEYCODE_BACK) && b) {
            mWebView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.destroy();
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initSetting(Context context, WebView webView) {
        WebSettings webSettings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setTextZoom(100);
        webSettings.setAppCacheMaxSize(10 * 1024 * 1024);
        webSettings.setAppCacheEnabled(true);
        webSettings.setAppCachePath(context.getDir("appCache", 0).getPath());
        webSettings.setAllowFileAccess(true);
        webSettings.setSavePassword(false);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    }
}
